﻿using System.Windows;
using System.Windows.Controls;

namespace LpbPrj.Client.Controls;

/// <summary>
/// 4个uint组成的一组控件
/// </summary>
public class ThicknessEx : Control
{
    public Thickness Thickness { get => (Thickness)GetValue(ThicknessProperty); set => SetValue(ThicknessProperty, value); }
    public static readonly DependencyProperty ThicknessProperty = DependencyProperty.Register(
            nameof(Thickness), typeof(Thickness), typeof(ThicknessEx), new FrameworkPropertyMetadata(new Thickness(0, 0, 0, 0), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnThicknessChanged));
    private static void OnThicknessChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var dp = d as ThicknessEx;
        Thickness newValue = (Thickness)e.NewValue;

        if (newValue.Left != dp.Left) dp.Left = (int)newValue.Left;
        if (newValue.Right != dp.Right) dp.Right = (int)newValue.Right;
        if (newValue.Top != dp.Top) dp.Top = (int)newValue.Top;
        if (newValue.Bottom != dp.Bottom) dp.Bottom = (int)newValue.Bottom;
    }


    public int Left { get => (int)GetValue(LeftProperty); set => SetValue(LeftProperty, value); }
    public static readonly DependencyProperty LeftProperty = DependencyProperty.Register(
            nameof(Left), typeof(int), typeof(ThicknessEx), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnLeftChanged));
    private static void OnLeftChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var dp = d as ThicknessEx;

        var newValue = (int)e.NewValue;
        var oldValue = (int)e.OldValue;
        if (newValue != oldValue)
        {
            dp.Thickness = new Thickness(newValue, dp.Top, dp.Right, dp.Bottom);
        }
    }

    public int Top { get => (int)GetValue(TopProperty); set => SetValue(TopProperty, value); }
    public static readonly DependencyProperty TopProperty = DependencyProperty.Register(
            nameof(Top), typeof(int), typeof(ThicknessEx), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTopChanged));
    private static void OnTopChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var dp = d as ThicknessEx;

        var newValue = (int)e.NewValue;
        var oldValue = (int)e.OldValue;
        if (newValue != oldValue)
        {
            dp.Thickness = new Thickness(dp.Left, newValue, dp.Right, dp.Bottom);
        }
    }

    public int Right { get => (int)GetValue(RightProperty); set => SetValue(RightProperty, value); }
    public static readonly DependencyProperty RightProperty = DependencyProperty.Register(
            nameof(Right), typeof(int), typeof(ThicknessEx), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnRightChanged));
    private static void OnRightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var dp = d as ThicknessEx;

        var newValue = (int)e.NewValue;
        var oldValue = (int)e.OldValue;
        if (newValue != oldValue)
        {
            dp.Thickness = new Thickness(dp.Left, dp.Top, newValue, dp.Bottom);
        }
    }

    public int Bottom { get => (int)GetValue(BottomProperty); set => SetValue(BottomProperty, value); }
    public static readonly DependencyProperty BottomProperty = DependencyProperty.Register(
            nameof(Bottom), typeof(int), typeof(ThicknessEx), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnBottomChanged));
    private static void OnBottomChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var dp = d as ThicknessEx;

        var newValue = (int)e.NewValue;
        var oldValue = (int)e.OldValue;
        if (newValue != oldValue)
        {
            dp.Thickness = new Thickness(dp.Left, dp.Top, dp.Right, newValue);
        }
    }

}
